//
//  NetService.m
//  20240823
//
//  Created by 张伟 on 2024/8/26.
//

#import "NetService.h"
#import "ApiPath.h"
#import "Requester.h"

@implementation NetService

+(void)saveUserInfo:(NSDictionary *)data {
    UserModel * model = [UserModel mj_objectWithKeyValues:data];
    UserModel.shared.userId = model.userId;
    UserModel.shared.account = model.account;
    UserModel.shared.password = model.password;
    UserModel.shared.isLogin = true;
    
    [NSUserDefaults.standardUserDefaults setValue:model.account forKey:accountKey];
    [NSUserDefaults.standardUserDefaults setValue:model.password forKey:passwordKey];
    [NSUserDefaults.standardUserDefaults synchronize];
}

+ (void)requestLogin:(NSString *)account password:(NSString *)pwd successHandle:(void (^)(void))success fail:(void (^)(void))error {
    
    NSDictionary * parame = @{@"account":account,@"password":pwd};
    Apis * api = [Apis initWithPath:ApiPath.loginAccount andMethod:RequestMethodPost andParame:parame];
    
    MJWeakSelf
    [Requester.shared sendRequest:api successHandle:^(ResultData * _Nonnull data) {
        [weakSelf saveUserInfo:(NSDictionary *)data.data];
        if (success) {
            success();
        }
    } failHandle:^(NSString * _Nonnull str) {
        if (error) {
            error();
        }
    }];
    
}

+ (void)requestRegister:(NSString *)account password:(NSString *)pwd successHandle:(void (^)(void))success fail:(void (^)(void))error {
    
    NSDictionary * parame = @{@"account":account,@"password":pwd};
    Apis * api = [Apis initWithPath:ApiPath.registerAccount andMethod:RequestMethodPost andParame:parame];
    
    MJWeakSelf
    [Requester.shared sendRequest:api successHandle:^(ResultData * _Nonnull data) {
        [weakSelf saveUserInfo:(NSDictionary *)data.data];
        if (success) {
            success();
        }
    } failHandle:^(NSString * _Nonnull str) {
        if (error) {
            error();
        }
    }];
    
}

+ (void)requestAddCallRecord:(NSDictionary *)parame successHandle:(void (^)(void))success fail:(void (^)(void))error {
    Apis * api = [Apis initWithPath:ApiPath.addCallRecord andMethod:2 andParame:parame];
    [Requester.shared sendRequest:api successHandle:^(ResultData * _Nonnull data) {
        if (success) {
            success();
        }
    } failHandle:^(NSString * _Nonnull str) {
        if (error) {
            error();
        }
    }];
}

+ (void)requestCallRecordList:(NSDictionary *)parame successHandle:(void (^)(NSArray<CallRecordModel *> * _Nonnull))success fail:(void (^)(void))error {
    Apis * api = [Apis initWithPath:ApiPath.callRecordList andMethod:2 andParame:parame];
    [Requester.shared sendRequest:api successHandle:^(ResultData * _Nonnull data) {
        NSArray * array = [CallRecordModel mj_objectArrayWithKeyValuesArray:data.data];
        if (success) {
            success(array);
        }
    } failHandle:^(NSString * _Nonnull str) {
        if (error) {
            error();
        }
    }];
}

+ (void)requestSendMessage:(NSDictionary *)parame successHandle:(void (^)(void))success fail:(void (^)(void))error {
    Apis * api = [Apis initWithPath:ApiPath.sendMessage andMethod:2 andParame:parame];;
    [Requester.shared sendRequest:api successHandle:^(ResultData * _Nonnull data) {
        if (success) {
            success();
        }
    } failHandle:^(NSString * _Nonnull str) {
        if (error) {
            error();
        }
    }];
}

+ (void)requestMessageList:(NSDictionary *)parame successHandle:(void (^)(NSArray<MessageModel *> * _Nonnull))success fail:(void (^)(void))error {
    Apis * api = [Apis initWithPath:ApiPath.messageList andMethod:2 andParame:parame];;
    [Requester.shared sendRequest:api successHandle:^(ResultData * _Nonnull data) {
        NSArray * array = [MessageModel mj_objectArrayWithKeyValuesArray:data.data];
        if (success) {
            success(array);
        }
    } failHandle:^(NSString * _Nonnull str) {
        if (error) {
            error();
        }
    }];
}

+ (void)requestDeactivateAccount:(NSDictionary *)parame successHandle:(void (^)(void))success fail:(void (^)(void))error {
    Apis * api = [Apis initWithPath:ApiPath.deactivateAccount andMethod:2 andParame:parame];
    [Requester.shared sendRequest:api successHandle:^(ResultData * _Nonnull data) {
        if (success) {
            success();
        }
    } failHandle:^(NSString * _Nonnull str) {
        if (error) {
            error();
        }
    }];
}

@end
